<?php

namespace App\Http\Controllers;

use Admin\Repositories\Contracts\AdminInterface;
use App\Http\Resources\BaseResource;
use App\Http\Resources\ErrorResource;
use App\Repositories\Contracts\AdminPlatformInterface;
use Illuminate\Http\Request;
use MakeRep\Controllers\BaseController;
use MakeRep\Resources\BaseCollection;
use MakeRep\Services\ParamService;

class AdminPlatformController extends BaseController
{

    private $adminInterface;
    public function __construct(AdminPlatformInterface $adminPlatformInterface,AdminInterface $adminInterface)
    {
        $this->interface = $adminPlatformInterface;
        $this->adminInterface = $adminInterface;
    }

    /**
     * 添加管理员OTA权限
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View|mixed
     */
    public function add()
    {
        $admin = $this->adminInterface->findById(request()->id);
        $adminPlatform = $this->interface->get(['admin_id'=>request()->id]);

        return view('adminplatform.add',['admin'=>$admin,'platform'=>$adminPlatform->pluck('platform')->toArray()]);
    }

    public function ajaxAdd(Request $request)
    {
        $res = $this->interface->delete(['admin_id'=>$request->admin_id_platform]);

        if ($request->platform) {

            $data = [];
            $createdAt = date('Y-m-d H:i:s');
            foreach ($request->platform as $value) {

                $data[] = [
                    'admin_id'  => $request->admin_id_platform,
                    'platform'  => $value,
                    'created_at'=> $createdAt,
                    'updated_at'=> $createdAt,
                ];

            }

            $res = $this->interface->insert($data);
        }

        if ($res) {

            return new BaseResource([]);
        }

        return new ErrorResource([]);
    }


    /**
     * 列表
     * @param Request $request
     * @return BaseCollection
     */
    public function ajaxLists(Request $request){

        $pageSize = $request->page_size ? : 10;
        $orderBy = $request->order_by ? : 'desc';
        $res = $this->adminInterface->pageLists(ParamService::createCondition($request,isset($this->interface->fillable) ? $this->interface->fillable : []),'*',$pageSize,'id',$orderBy);
        return new BaseCollection($res);
    }



}